<template>
    <div>
        <div class="page-title">编辑信息</div>
        <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="供货公司">
                <el-input ref="ghgs" v-model="form.ghgs" placeholder="请输入供货公司"></el-input>
            </el-form-item>

            <el-form-item label="采购单价(元)">
                <el-input ref="cgdj" v-model="form.cgdj" placeholder="请输入采购单价(元)"></el-input>
            </el-form-item>

            <el-form-item label="采购数量(吨)">
                <el-input ref="cgsl" v-model="form.cgsl" placeholder="请输入采购数量(吨)"></el-input>
            </el-form-item>

            <el-form-item label="采购价款(元)">
                <el-input ref="cgjk" v-model="form.cgjk" placeholder="请输入采购价款(元)"></el-input>
            </el-form-item>

            <el-form-item label="收货单位">
                <el-input ref="shdw" v-model="form.shdw" placeholder="请输入收货单位"></el-input>
            </el-form-item>

            <el-form-item label="销售单价（元）">
                <el-input ref="xsdj" v-model="form.xsdj" placeholder="请输入销售单价（元）"></el-input>
            </el-form-item>

            <el-form-item label="销售数量（吨）">
                <el-input ref="xssl" v-model="form.xssl" placeholder="请输入销售数量（吨）"></el-input>
            </el-form-item>

            <el-form-item label="销售价款（元）">
                <el-input ref="xsjk" v-model="form.xsjk" placeholder="请输入销售价款（元）"></el-input>
            </el-form-item>

            <el-form-item label="终端用户">
                <el-input ref="zdyh" v-model="form.zdyh" placeholder="请输入终端用户"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="success" icon="el-icon-check" @click="submit">修改</el-button>
                <el-button icon="el-icon-close" @click="cancel">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import {
        get,
        post
    } from '@/network/request'
    import {
        __URL__
    } from '@/main'

    export default {
        name: 'EditHome',
        data() {
            return {
                form: {
                    id: this.$route.params.id,
                    ghgs: '',
                    cgdj: '',
                    cgsl: '',
                    cgjk: '',
                    shdw: '',
                    xsdj: '',
                    xssl: '',
                    xsjk: '',
                    zdyh: ''


                },
                options: [], // 分类数组
                uploadFile: __URL__ + '/uploadFile', // 上传文件地址
                fileData: {
                    ext: '' // 文件后缀
                }
            }
        },
        mounted() {
            this.$nextTick(() => {
                // 获取信息
                get('/homeList', {
                    id: this.$route.params.id,
                }).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.form.ghgs = result.data.ghgs
                        this.form.cgdj = result.data.cgdj
                        this.form.cgsl = result.data.cgsl
                        this.form.cgjk = result.data.cgjk
                        this.form.shdw = result.data.shdw
                        this.form.xsdj = result.data.xsdj
                        this.form.xssl = result.data.xssl
                        this.form.xsjk = result.data.xsjk
                        this.form.zdyh = result.data.zdyh
                    } else {
                        this.$router.push('/404')
                    }
                }).catch()
            })
        },
        methods: {
            // 上传准备
            beforeUpload(file) {
                if (['application/zip', 'application/rar', 'application/x-zip-compressed'].indexOf(file.type) == -1) {
                    this.$message.error({
                        message: '请上传正确的文件格式！',
                        offset: 70
                    })
                    return false
                }

                if (file.type == 'application/zip' || file.type == 'application/x-zip-compressed') {
                    this.fileData.ext = 'zip';
                }

                if (file.type == 'application/rar') {
                    this.fileData.ext = 'rar';
                }

                const size = file.size / 1024 / 1024 < 2
                if (!size) {
                    this.$message.error({
                        message: '文件大小不能超过 2MB！',
                        offset: 70
                    })
                    return false
                }
            },
            // 上传成功
            uploadSuccess(res) {
                this.form.file = res.data
                this.$notify.success({
                    title: '系统提示',
                    message: '文件上传成功',
                    offset: 70
                })
            },
            // 提交
            submit() {
                // if (this.form.title == '') {
                //     this.$notify.info({
                //         title: '系统提示',
                //         message: '请输入指南标题',
                //         offset: 70
                //     })
                //     this.$refs['title'].focus()
                //     return false
                // }

                // if (this.form.type == '') {
                //     this.$notify.info({
                //         title: '系统提示',
                //         message: '请选择分类',
                //         offset: 70
                //     })
                //     this.$refs['type'].focus()
                //     return false
                // }

                post('/saveData', this.form).then(res => {
                    const result = res.data
                    if (result.code == 200) {
                        this.$notify.success({
                            title: '系统提示',
                            message: '操作成功',
                            offset: 70
                        })
                        this.$router.push('/home')
                    }
                }).catch()
            },
            // 取消
            cancel() {
                this.$router.push('/guide')
            }
        }
    }
</script>

<style>
</style>